package com.wxtt.android.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wxtt.android.R;
import com.wxtt.android.animation.BaseEffects;
import com.wxtt.android.animation.Fill;

/**
 * 说明：提示对话框
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2016/1/1 21:58
 * <p/>
 * 版本：verson 1.0
 */
public class TipDialog extends BaseDialog{

    private BaseEffects type = null;
    private View view;
    private TextView tv_tip,tv_msg,tv_sure,tv_cancel;

    public TipDialog(Context context) {
        super(context);
    }

    @Override
    public void onInit() {
        view = getDialogView();
        tv_sure = (TextView) view.findViewById(R.id.tv_sure);
        tv_tip = (TextView) view.findViewById(R.id.tv_tip);
        tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if (type == null) {
                    type = new Fill();
                }
                start(type);
            }
        });
    }

    private void start(BaseEffects type){
        type.start(view);
    }

    @Override
    public int setDialogView() {
        return R.layout.dialog_tip;
    }

    private void setText(String msg,TextView tv){
        if (TextUtils.isEmpty(msg)){
            tv.setVisibility(View.GONE);
        }else {
            tv.setVisibility(View.VISIBLE);
            tv.setText(msg);
        }
    }

    /**
     * 说明：设置标题提示
     * @param title
     * @return
     */
    public TipDialog setTip(String title){
        setText(title,tv_tip);
        return this;
    }

    /**
     * 说明：设置对话框内容
     * @param msg
     * @return
     */
    public TipDialog setContent(String msg){
        setText(msg,tv_msg);
        return this;
    }

    /**
     * 说明：设置确定按钮文字
     * @param text
     * @return
     */
    public TipDialog setSureText(String text){
        setText(text,tv_sure);
        return this;
    }

    /**
     * 说明：设置确定按钮文字颜色
     * @param color
     * @return
     */
    public TipDialog setSureTextColor(int color){
        tv_sure.setTextColor(color);
        return this;
    }

    /**
     * 说明：设置取消按钮文字
     * @param text
     * @return
     */
    public TipDialog setCancelText(String text){
        setText(text, tv_cancel);
        return this;
    }

    /**
     * 说明：设置取消按钮文字颜色
     * @param color
     * @return
     */
    public TipDialog setCancelTextColor(int color){
        tv_cancel.setTextColor(color);
        return this;
    }

    /**
     * 说明：设置确认按钮监听事件
     * @param listener
     * @return
     */
    public TipDialog setClickListener(final TipClickListener listener){
        if (listener != null){
            tv_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!listener.onSure()){
                        dismiss();
                    }
                }
            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!listener.onCancel()){
                        dismiss();
                    }
                }
            });
        }
        return this;
    }

    /**
     * 说明：隐藏取消按钮
     * @return
     */
    public TipDialog hiddenCancel(){
        tv_cancel.setVisibility(View.GONE);
        return this;
    }

    /**
     * 说明：提示框监听器，当返回true时，关闭当前提示框，返回false不关闭
     */
    public interface TipClickListener{
        boolean onSure();
        boolean onCancel();
    }
}
